package com.bookmall.cart.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 解决feign调用丢失请求头和请求上下文
 * @Author 胡菜鸡
 * @Create 2022-04-22-10:02
 */
@Configuration
public class FeignConfig {
//    @Bean("requestInterceptor")
//    public RequestInterceptor requestInterceptor(){
//        return new RequestInterceptor(){
//            @Override
//            public void apply(RequestTemplate template) {
//                System.out.println("feign远程之前的requestInterceptor.apply方法");
//                //1、使用RequestContextHolder拿到刚进来的这个请求
//                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//                HttpServletRequest request = attributes.getRequest();
//                if(request != null){
//                    //2、给新请求同步请求头数据
//                    String cookie = request.getHeader("Cookie");
//                    template.header("Cookie", cookie);
//                }
//            }
//        };
//    }
}
